package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.CustomerPart;
import com.efast.dmom.mes.bean.Part;
import com.efast.dmom.mes.bean.PartTree;
import com.efast.dmom.mes.bean.ProductRoute;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.SupplierPart;
import com.efast.dmom.mes.datahandlers.entity.BomItemDataHandler;
import com.efast.dmom.mes.datahandlers.entity.CustomerPartDataHandler;
import com.efast.dmom.mes.datahandlers.entity.PartDataHandler;
import com.efast.dmom.mes.datahandlers.entity.ProductRouteDataHandler;
import com.efast.dmom.mes.datahandlers.entity.SupplierPartDataHandler;
import com.efast.dmom.mes.entity.PartEntity;
import com.efast.dmom.mes.service.PartService;

@Service
public class PartServiceImpl implements PartService {

	@Autowired
	private PartDataHandler partHandler;
	@Autowired
	private BomItemDataHandler bomItemHandler;
	@Autowired
	private SupplierPartDataHandler supplierPartDataHandler;
	@Autowired
	private CustomerPartDataHandler customerPartDataHandler;
	@Autowired
	private ProductRouteDataHandler productRouteDataHandler;
	
	
	@Override
	public ResultBean save(Part part) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PartEntity entity = part.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String partNo = entity.getPartNo();
		String partRevision = entity.getRevision();
		if(partNo==null || partRevision==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("partNo和partRevision为必填项!");
			return resultBean;
		}
		Part partByPartNo = partHandler.getPartByPartNo(partNo, partRevision);
		if(partByPartNo.getInnerData()!=null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("partNo和partRevision已存在!");
			return resultBean;
		}
//		if (!StringVerifyUtil.isstring(partNo)){
//		    resultBean.setSuccess(false);
//		    resultBean.setMessage("物料编号不是由英文、字母、下划线组成!!");
//		    return resultBean;
//		 }
		partHandler.save(part);
		resultBean.setSuccess(true);
		resultBean.setMessage("添加成功!");
		return resultBean;
	}

	@Override
	public ResultBean update(Part part) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PartEntity entity = part.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String partNo = entity.getPartNo();
		String partRevision = entity.getRevision();
		if(partNo==null || partRevision==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("partNo和partRevision为必填项!");
			return resultBean;
		}
		Part byId = partHandler.getById(entity.getId());
		
		if(byId==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该物料不存在!");
			return resultBean;
		}
		
		if(!partNo.equals(byId.getInnerData().getPartNo()) || !partRevision.equals(byId.getInnerData().getRevision())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("物料号和物料版本不能修改!");
			return resultBean;
		}
		partHandler.update(part);
		resultBean.setSuccess(true);
		resultBean.setMessage("修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(Part part) {
		ResultBean resultBean = new ResultBean(true);
		PartEntity entity = part.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String partNo = entity.getPartNo();
		String partRevision = entity.getRevision();
		if(partNo==null || partRevision==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("partNo和partRevision为必填项!");
			return resultBean;
		}
		// 校验物料是否被单据引用
		ResultBean validatePartDelete = validatePartDelete(part);
		if (!validatePartDelete.success) {
			return validatePartDelete;
		}
		Part byId = partHandler.getById(entity.getId());
		if(byId.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该物料不存在!");
			return resultBean;
		}
		
		//判断是否被引用
		
		int count = bomItemHandler.getCountByPartAndRevision(partNo, partRevision);
		
		if(count >0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该物料被BOM引用，不能删除!");
			return resultBean;
		}
		
		List<SupplierPart> supplierPartByPartId = supplierPartDataHandler.getSupplierPartByPartId(entity.getId());
		if(supplierPartByPartId !=null && supplierPartByPartId.size()>0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该物料被供应商引用，不能删除!");
			return resultBean;
		}
		List<CustomerPart> customerPartByPartId = customerPartDataHandler.getCustomerPartByPartId(entity.getId());
		if(customerPartByPartId !=null && customerPartByPartId.size()>0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该物料被客户引用，不能删除!");
			return resultBean;
		}
		List<ProductRoute> byPartId = productRouteDataHandler.getByPartId(entity.getId());
		if(byPartId !=null && byPartId.size()>0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该物料被工艺路线引用，不能删除!");
			return resultBean;
		}
		
		
		partHandler.delete(part);
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功!");
		return resultBean;
	}

	@Override
	public Part getByPartNoAndRevision(String partNo, String partRevision) {
		return partHandler.getPartByPartNo(partNo, partRevision);
	}

	@Override
	public Part getById(long partId) {
		return partHandler.getById(partId);
	}

	@Override
	public List<Part> getAllPart() {
		return partHandler.getAllPart();
	}

	@Override
	public PartTree getPartByPartNoAndRevision(Part part) {
		if (part.getLength() == 0) {
			part.setLength(10);
		}
		if (part.getStart() == 1) {
			part.setStart(0);
		} else if(part.getStart() > 1){
			part.setStart((part.getStart()-1)*part.getLength());
		}
		List<PartEntity> partList = partHandler.getPartByPartNoAndRevision(part);
		part.setStart(0);
		part.setLength(0);
		List<PartEntity> partSize = partHandler.getPartByPartNoAndRevision(part);
		PartTree partTree = new PartTree();
		if (partList != null && partSize != null) {
			partTree.setCountSize(partSize.size());
			partTree.setPart(partList);
		}
		return partTree;
	}

	@Override
	public ResultBean validatePartDelete(Part part) {
		return partHandler.validatePartDelete(part);
	}

	@Override
	public ResultBean batchDelete(List<Part> list) {
		ResultBean resultBean = new ResultBean(true);
		for (Part part : list) {
			ResultBean delete = delete(part);
			if (!delete.success) {
				return delete;
			}
		}
		return resultBean;
	}
	
}
